﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.Productions;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.Productions
{
    public class ProWorkhoursTypeServices : IProWorkhoursTypeServices
    {
        public ProWorkhoursTypeServices(IBaseRepository db)
        {
            Db = db;
        }
        public IBaseRepository Db { get; }
        public async Task<bool> AddAsync(ProWorkhoursType model)
        {
            var result = await Db.InsertAsync(model);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete ProWorkhoursType where Id in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete ProWorkhoursType where Id = @Id";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }

        public async Task<IEnumerable<ProWorkhoursType>> GetAllAsync()
        {
            string sql = "select * from ProWorkhoursType";
            var list = await Db.QueryAllAsync<ProWorkhoursType>(sql);
            return list;
        }

        public async Task<IEnumerable<ProWorkhoursType>> GetAsync(int PageIndex, int PageSize, string WorkhoursType)
        {
            string sql = "select * from ProWorkhoursType where 1=1";
            if (!string.IsNullOrEmpty(WorkhoursType))
            {
                sql += " and WorkhoursType like @WorkhoursType";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";
            var list = await Db.QueryAsync<ProWorkhoursType>(sql, new { PageIndex, PageSize, WorkhoursType = $"%{WorkhoursType}%" });
            return list;
        }

        public async Task<int> GetCountAsync(string WorkhoursType)
        {
            string sql = "select count(*) from ProWorkhoursType where 1=1";
            if (!string.IsNullOrEmpty(WorkhoursType))
            {
                sql += " and WorkhoursType like @WorkhoursType";
            }
            var list = await Db.GetRecoreCountAsync(sql, new {  WorkhoursType = $"%{WorkhoursType}%" });
            return list;
        }

        public async Task<bool> UpdateAsync(ProWorkhoursType model)
        {
            var result = await Db.UpdateAsync(model);
            return result;
        }
    }
}
